package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.Service.OrderService;
import com.woniu.annotation.CheckPermission;
import com.woniu.util.JsonResult;
import com.woniu.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: Gwj
 * @Date: 2024/7/9 12:00
 */
@RestController
@Slf4j
@RequestMapping("/order")
@Api(value = "订单功能", tags = "所有的订单功能")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 分页查询订单
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/findAllOrderByPage")
    @ApiOperation(value = "分页查询订单接口")
    @CheckPermission("order:select")
    public JsonResult<PageInfo<OrderVo>> findAllOrderByPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) throws Exception {
        PageInfo<OrderVo> allOrderByPage = orderService.findAllOrderByPage(pageNum, pageSize);
        return JsonResult.build(200, "success", allOrderByPage);
    }

    @GetMapping("/findOrderByCondition")
    @ApiOperation(value = "条件查询订单")
    @CheckPermission("order:select")
    public JsonResult<PageInfo<OrderVo>> findOrderByCondition(@RequestParam(defaultValue = "1") int pageNum,
                                                              @RequestParam(defaultValue = "5") int pageSize,
                                                              @RequestParam(value = "flowerName", required = false) String flowerName,
                                                              @RequestParam(value = "nickName", required = false) String nickName,
                                                              @RequestParam(value = "startTime", required = false) String startTime,
                                                              @RequestParam(value = "endTime", required = false) String endTime,
                                                              @RequestParam(value = "orderId", required = false) String orderId,
                                                              @RequestParam(value = "status", required = false) String status
    ) throws Exception {
        log.info("================================status=" + status + "startTime=" + startTime + "endTime=" + endTime +  "orderId=" + orderId +"nickName="+nickName+"flowerName="+flowerName);
        OrderVo orderVo = new OrderVo();
        orderVo.setFlowerName(flowerName);
        orderVo.setNickName(nickName);
        orderVo.setStartTime(startTime);
        orderVo.setEndTime(endTime);
        orderVo.setOrderId(orderId);
        orderVo.setStatus(status);
        PageInfo<OrderVo> orderVos = orderService.findOrdersByConditions(orderVo, pageNum, pageSize);
        return JsonResult.build(200, "success", orderVos);
    }
}
